package com.vcaresolution.logistic.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.RedirectView;

import com.vcaresolution.logistic.form.SectorForm;
import com.vcaresolution.logistic.to.SectorTO;
import com.vcaresolution.logistic.util.ApplicationConstants;

/**
 * <code>SectorController</code> is used to track all login related actions
 *
 * @author Snehal Shah
 * @version 1.0
 * @since ShipSoft 1.0
 */
@Controller
public class SectorController extends BaseController{

	@ModelAttribute(value=ApplicationConstants.SECTORFORM_MODELATTRIBUTE)
	public SectorForm createForm(){
		return new SectorForm();
	}
	
	/**
	 * This method is called when the GET request has been made to the ADDSector Request
	 */
	@RequestMapping(value=ApplicationConstants.ADD_SECTOR_URL,method = RequestMethod.GET)
	public ModelAndView loadAddSector() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName(ApplicationConstants.ADD_SECTOR_VIEW);
		modelAndView.addObject(ApplicationConstants.SECTORFORM_MODELATTRIBUTE, new SectorForm());
		return modelAndView;
	}
	
	/**
	 * This method is called when the POST request has been made to the ADDSector Request
	 */
	@RequestMapping(value=ApplicationConstants.ADD_SECTOR_URL,method = RequestMethod.POST)
	public ModelAndView processAddSector(@Valid SectorForm companyForm, final BindingResult result,final HttpServletRequest request,final RedirectAttributes redirectAttrs) {
		ModelAndView modelAndView = new ModelAndView();
		
		System.out.println("Inside Controller :: "+result.hasErrors());
		
		if( !result.hasErrors() ){
			getServiceRegistry().getSectorService().add(companyForm);
			
			modelAndView.setView(new RedirectView(ApplicationConstants.LIST_SECTOR_URL, true, true, false));
		}
		
		return modelAndView;
	}
	
	/**
	 * This method is called when the GET request has been made to the ListSector Request
	 */
	@RequestMapping(value=ApplicationConstants.LIST_SECTOR_URL,method = RequestMethod.GET)
	public ModelAndView list() {
		final ModelAndView modelAndView = new ModelAndView();
		
		List<SectorTO> companyList = getServiceRegistry().getSectorService().findByAll();
		
		modelAndView.addObject(ApplicationConstants.SECTORLIST_MODELATTRIBUTE, companyList);
		modelAndView.setViewName(ApplicationConstants.LIST_SECTOR_VIEW);
		return modelAndView;
	}
	
	/**
	 * This method is called when the GET request has been made to the ADDSector Request
	 */
	@RequestMapping(value=ApplicationConstants.EDIT_SECTOR_URL,method = RequestMethod.GET)
	public  ModelAndView loadEditSector(@RequestParam(value="sectorId") String sectorID) {
		ModelAndView modelAndView = new ModelAndView();
		
		Object sectorObject = getServiceRegistry().getSectorService().findById(Integer.parseInt(sectorID));
		
		modelAndView.addObject(ApplicationConstants.SECTORFORM_MODELATTRIBUTE, sectorObject);
		modelAndView.setViewName(ApplicationConstants.EDIT_SECTOR_VIEW);
		
		return modelAndView;
	}
	
	/**
	 * This method is called when the POST request has been made to the ADDSector Request
	 */
	@RequestMapping(value=ApplicationConstants.EDIT_SECTOR_URL,method = RequestMethod.POST)
	public ModelAndView processEditSector(@Valid SectorForm sectorForm, final BindingResult result,final HttpServletRequest request,final RedirectAttributes redirectAttrs) {
		ModelAndView modelAndView = new ModelAndView();
		
		System.out.println("Inside Controller :: "+result.hasErrors());
		
		if( !result.hasErrors() ){
			getServiceRegistry().getSectorService().update(sectorForm);
			
			modelAndView.setView(new RedirectView(ApplicationConstants.LIST_SECTOR_URL, true, true, false));
		}
		
		return modelAndView;
	}
}
